package com.example.administrator.szb.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;

import java.util.ArrayList;

/**
 * Created by stone on 2018/1/8.
 */

public class WrapLayout  extends LinearLayout {
    //放置TAG名字的集合
    public ArrayList<String> mTagNames = new ArrayList<String>();
    //每一行 LinearLayout 集合
    public ArrayList<LinearLayout> mLins = new ArrayList<LinearLayout>();
    //布局泵
    public LayoutInflater mInflater;

    public Context context ;



    public WrapLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.setOrientation(this.VERTICAL);
        this.context = context;
        mInflater = LayoutInflater.from(context);
        //默认有一行
        addLin();
    }

    public WrapLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.setOrientation(this.VERTICAL);
        this.context = context;
        mInflater = LayoutInflater.from(context);
        //默认有一行
        addLin();
    }

    public WrapLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.setOrientation(this.VERTICAL);
        this.context = context;
        mInflater = LayoutInflater.from(context);
        //默认有一行
        addLin();
    }




    /**
     * 增加列
     *
     *2015-10-9
     *by chenxiu
     */
    public void addTag(String tag) {
        // TODO Auto-generated method stub
        //先构建一个tag标签view
        View view = mInflater.inflate(R.layout.item_text, null);
        view.setTag(tag);
        TextView tagName = (TextView) view.findViewById(R.id.textViews);
        tagName.setText(tag);

        //tag点击事件 当点击时  返回tag名
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });
        int w = getWidth(view);

        //获取屏幕宽度和最新一行的宽度
        WindowManager wm = (WindowManager)context
                .getSystemService(Context.WINDOW_SERVICE);
        int linW = getWidth(mLins.get(mLins.size()-1));
        int whosW = wm.getDefaultDisplay().getWidth();

        //Log.e("TAG", "屏幕宽度："+whosW+",行宽="+linW+",字宽="+w);
        //判断当前最新一行数据是否达到屏幕宽度
        if((linW+w)<whosW)
        {
            //执行添加view操作
            mLins.get(mLins.size()-1).addView(view);
        }
        else
        {
            //超过了就递归 新增一行 然后再加view
            addLin();
            addTag(tag);
        }
    }

    /**
     * 增加行
     *
     *2015-10-9
     *by chenxiu
     */
    public void addLin()
    {
        LinearLayout lin = new LinearLayout(context);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lin.setOrientation(LinearLayout.HORIZONTAL);
        lin.setLayoutParams(params);
        mLins.add(lin);
        this.addView(lin);
    }


    /**
     * 获取宽度
     *
     *2015-10-9
     *by chenxiu
     */
    public int  getWidth(View view)
    {		int width = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        view.measure(width,height);

        int h=view.getMeasuredHeight();

        int w=view.getMeasuredWidth();
        return w;

    }
    /**
     * 获取高度
     *
     *2015-10-9
     *by chenxiu
     */
    public int  getHeight(View view)
    {		int width = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        view.measure(width,height);

        int h=view.getMeasuredHeight();

        int w=view.getMeasuredWidth();
        return h;

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
